package com.kehutong.common;

import org.coraframework.converter.util.StringUtil;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * Author:  peter
 * Date:    2020/7/16
 * Function:处理java8时间类型转字符串格式
 */
public class Java8TimeUtils {

    public static final String DEFAULT_LOCAL_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_LOCAL_DATE_TIME = "yyyyMMddHHmmss";
    public static final String DATE_YYYY_MM_DD = "yyyy-MM-dd";

    public static String parseString(LocalDateTime ldt) {
        if (ldt == null) {
            return null;
        }
        return ldt.format(DateTimeFormatter.ofPattern(DEFAULT_LOCAL_DATE_TIME_PATTERN));
    }

    /**
     * https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7
     * 微信支付完成通知
     * 支付完成时间，格式为yyyyMMddHHmmss，如2009年12月25日9点10分10秒表示为20091225091010
     */
    public static LocalDateTime parseFromTimeEnd(String timeEnd) {
        return LocalDateTime.of(
                Integer.parseInt(timeEnd.substring(0, 4)),
                Integer.parseInt(timeEnd.substring(4, 6)),
                Integer.parseInt(timeEnd.substring(6, 8)),
                Integer.parseInt(timeEnd.substring(8, 10)),
                Integer.parseInt(timeEnd.substring(10, 12)),
                Integer.parseInt(timeEnd.substring(12, 14))
        );
    }

    /**
     * 长时间：2021-12-10 12:20:50
     * @return
     */
    public static String dateLong(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_LOCAL_DATE_TIME_PATTERN);
        return sdf.format(date);
    }

    /**
     * 长时间：20211210122050
     * @return
     */
    public static String dateLongTime(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_LOCAL_DATE_TIME);
        return sdf.format(date);
    }

    /**
     * 短时间：2021-12-10
     * @return
     */
    public static String dateShort(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_YYYY_MM_DD);
        return sdf.format(date);
    }

    /**
     * 短时间：2021-12-10
     * @return
     */
    public static String dateShort(String date){
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_YYYY_MM_DD);
        return sdf.format(date);
    }

    /**
     * 时间处理 '2021-07-03T16:00:00.000Z'
     * @param date
     * @return
     */
    public static String formatStrDate(String date){
        if(StringUtil.isNotEmpty(date)){
            date = date.replace("T"," ").replace("0Z","");
        }
        return date;
    }

    /**
     * 时间处理 '2021-07-03T16:00:00.000Z'
     * @param date
     * @return
     */
    public static String subStrDate(String date){
        if(StringUtil.isNotEmpty(date)){
            date = date.substring(0,10);
        }
        return date;
    }

    /**
     * 时间处理 天数不能小于0
     * @param date
     * @return
     */
    public static Integer subDate(String date){

        int num = DailyUtil.intervalDays(date.substring(0,10));
        if(num <= 0){
            return 0;
        }

        return num;
    }

    public static void main(String[] args) {

        System.out.println("长时间："+dateLong());
        System.out.println("短时间："+dateShort());

        String str = "2021-05-05 23:59:59".substring(0,10);
        System.out.println("短时间====："+str);
        System.out.println("天数："+subDate(str));

        System.out.println("转时间："+formatStrDate("2021-07-03T16:00:00.000Z"));
        System.out.println("截取时间："+subStrDate("2021-07-03T16:00:00.000Z"));
    }

}
